<?php

//
function uid2code($a) {
    $code = (int)$a + pow(10, 8);
    return strtoupper(dechex($code));
}

//
function code2uid($a) {
    $code = hexdec(strtolower($a));
    return $code - pow(10, 8);
}

//v
function v2html($a) {
    if (!$a) {
        return '';
    }
    return '<i class="icon_v icon_v' . $a . '"></i>';
}

/**
 * 课程分类
 * @param $field
 * @return string
 */
function omni_classtype($field) {
    $fields = explode(",", $field);
    $fields[0] = $fields[0] ? $fields[0] : 0;
    $str = "<select name='info[{$fields[1]}]' id='{$fields[1]}'>\n";
    $str .= "<option value='0'>请选择</option>\n";

    $_classtypeArr = getcache(3360, 'linkage');
    $_classtypeArr = $_classtypeArr['data'];
    foreach ($_classtypeArr as $v) {
        if ($v['parentid'] != 0) {
            continue;
        }
        if ($fields[0] == $v['linkageid']) {
            $str .= "<option selected=''";
        } else {
            $str .= "<option";
        }
        $str .= " value='{$v['linkageid']}'>" . str_pad($v['linkageid'], 10, '.', STR_PAD_RIGHT) . $v['name'] . '</option>' . "\n";
    }
    $str .= '</select>';
    return $str;
}

/**
 * 课程标签
 * @param $field
 * @return string
 */
function omni_classtag($field) {
    $fields = explode(",", $field);
    $fields[0] = $fields[0] ? $fields[0] : 0;
    $str = "<select name='info[{$fields[1]}]' id='{$fields[1]}'>\n";
    $str .= "<option value='0'>请选择</option>\n";

    $_classtypeArr = getcache(3360, 'linkage');
    $_classtypeArr = $_classtypeArr['data'];
    foreach ($_classtypeArr as $v1) {
        if ($v1['parentid'] != 0) {
            continue;
        }
        $str .= "<optgroup label='{$v1['name']}'></optgroup>\n";
        foreach ($_classtypeArr as $v2) {
            if ($v2['parentid'] == $v1['linkageid']) {
                $str .= "<option>{$v2['name']}</option>\n";
            }
        }
    }
    $str .= '</select>';
    return $str;
}

/**
 * 多课
 * @param $field
 * @return string
 */
function omni_arr_class($field) {
    $js = JS_PATH2;
    $css = CSS_PATH2;

    $fields = explode("|", $field);
    $val_arr = $fields[0] ? $fields[0] : '';
    $val_arr = explode(",", $val_arr);

    $db = pc_base::load_model('lesson_model');
    $all = $db->listinfo('', '', 1, 500);

    if (!isset($_GET['id'])) {
        return '<a style="color:#f00;">完成发布后, 再进行小课设置</a>';
    }

    $url_arr = [
        'm' => 'content',
        'c' => 'lesson',
        'a' => 'init',
        'catid' => 8,
        'p_catid' => $_GET['catid'],
        'p_id' => $_GET['id'],
    ];
//    $url_arr = [
//        'm' => 'content',
//        'c' => 'content',
//        'a' => 'init',
//        'menuid' => '822',
//        'searchtype' => '3',
//        'catid' => '8',
//        'keyword' => $_GET['id'],
//    ];
    $url_arr = array_merge($_GET, $url_arr);

    $str = '<a style="color:#f00;" target="**admin.list.8**" href="index.php?' . http_build_query($url_arr) . '">点击进行详细设置</a>';
    return $str;
}

/**
 * 多课
 * @param $field
 * @return string
 */
function omni_arr_can() {
    $url_arr = [
        'm' => 'content',
        'c' => 'content',
        'a' => 'init',
        'catid' => 13,
        'p_catid' => $_GET['catid'],
        'p_id' => $_GET['id'],
        'pc_hash' => $_SESSION['pc_hash'],
    ];
   
    $_SESSION['p_id']=$_GET['id'];
    $url_arr = array_merge($_GET, $url_arr);
    $str = '<a style="color:#f00;" href="index.php?m=content&c=content&a=init&catid=13&p_id='.$_GET['id'].'&p_id='.$_GET['id'].'&pc_hash='.$_SESSION['pc_hash'].'">点击进行详细设置</a>';
    return $str;
}
function omni_arr_cases() {
    $url_arr = [
        'm' => 'content',
        'c' => 'content',
        'a' => 'init',
        'catid' => 14,
        'p_catid' => $_GET['catid'],
        'p_id' => $_GET['id'],
        'pc_hash' => $_SESSION['pc_hash'],
    ];
    $_SESSION['p_id']=$_GET['id'];
    $url_arr = array_merge($_GET, $url_arr);
    $str = '<a style="color:#f00;" href="index.php?m=content&c=content&a=init&catid=14&p_id='.$_GET['id'].'&p_id='.$_GET['id'].'&pc_hash='.$_SESSION['pc_hash'].'">点击进行详细设置</a>';
    return $str;
}
function omni_arr_teacher($field) {
    $js = JS_PATH2;
    $css = CSS_PATH2;

    $fields = explode("|", $field);
    $val_arr = $fields[0] ? $fields[0] : '';
    $val_arr = explode(",", $val_arr);

    $db = pc_base::load_model('preson_model');
    $all = $db->listinfo('', '', 1, 500);

    $str = "<input style='width:500px;' name='info[{$fields[1]}]' id='{$fields[1]}' value='{$fields[0]}'>\n";
    $str .= "<select multiple='multiple' id='mc_arr_teacher'>\n";
    foreach ($all as $v) {
        if (in_array($v['id'], $val_arr)) {
            $str .= "<option selected";
        } else {
            $str .= "<option";
        }
        $str .= " value='{$v['id']}'>" . str_pad($v['id'], 10, '.', STR_PAD_RIGHT) . $v['title'] . '</option>';
    }
    $str .= '</select>';
    $str .= <<<EOFILE
<link rel="stylesheet" type="text/css" href="{$css}multi-select.css">
<script type="text/javascript" src="{$js}jquery.multi-select.js"></script>
<script type="text/javascript">
$(function(){
    $('#mc_arr_teacher').multiSelect({
        'selectableHeader': '未加入',
        'selectionHeader': '已加入',
        'keepOrder': true,
        'afterSelect': function(v) {
            var val = $('#mc_arr_teacher').val();
            $('#{$fields[1]}').val(val);
        },
        'afterDeselect': function(v) {
            var val = $('#mc_arr_teacher').val();
            $('#{$fields[1]}').val(val);
        }
    });
});
</script>
EOFILE;
    return $str;
}

function omni_arr_student($field) {
    $js = JS_PATH2;
    $css = CSS_PATH2;

    $fields = explode("|", $field);
    $val_arr = $fields[0] ? $fields[0] : '';
    $val_arr = explode(",", $val_arr);

    $db = pc_base::load_model('preson_model');
    $all = $db->listinfo('', '', 1, 500);

    $str = "<input style='width:500px;' name='info[{$fields[1]}]' id='{$fields[1]}' value='{$fields[0]}'>\n";
    $str .= "<select multiple='multiple' id='mc_arr_student'>\n";
    foreach ($all as $v) {
        if (in_array($v['id'], $val_arr)) {
            $str .= "<option selected";
        } else {
            $str .= "<option";
        }
        $str .= " value='{$v['id']}'>" . str_pad($v['id'], 10, '.', STR_PAD_RIGHT) . $v['title'] . '</option>';
    }
    $str .= '</select>';
    $str .= <<<EOFILE
<script type="text/javascript">
$(function(){
    $('#mc_arr_student').multiSelect({
        'selectableHeader': '未加入',
        'selectionHeader': '已加入',
        'keepOrder': true,
        'afterSelect': function(v) {
            var val = $('#mc_arr_student').val();
            $('#{$fields[1]}').val(val);
        },
        'afterDeselect': function(v) {
            var val = $('#mc_arr_student').val();
            $('#{$fields[1]}').val(val);
        }
    });
});
</script>
EOFILE;
    return $str;
}


/**
 * 单个老师下拉
 * @param $field
 * @return string
 */
function omni_one_teacher($field) {
    $fields = explode(",", $field);
    $fields[0] = $fields[0] ? $fields[0] : 0;
    $str = "<select name='info[{$fields[1]}]' id='{$fields[1]}'>\n";
    $str .= "<option value='0'>请选择</option>";

    $db = pc_base::load_model('teacher_model');
    $all_teacher = $db->listinfo('', '', 1, 500);
    foreach ($all_teacher as $v) {
        if ($fields[0] == $v['id']) {
            $str .= "<option selected=''";
        } else {
            $str .= "<option";
        }
        $str .= " value='{$v['id']}'>" . str_pad($v['id'], 10, '.', STR_PAD_RIGHT) . $v['title'] . '</option>';
    }
    $str .= '</select>';
    return $str;
}

/**
 * 单个机构下拉
 * @param $field
 * @return string
 */
function omni_one_team($field) {
    $fields = explode(",", $field);
    $fields[0] = $fields[0] ? $fields[0] : 0;
    $str = "<select name='info[{$fields[1]}]' id='{$fields[1]}'>\n";
    $str .= "<option value='0'>请选择</option>";

    $db = pc_base::load_model('team_model');
    $all_teacher = $db->listinfo('', '', 1, 500);
    foreach ($all_teacher as $v) {
        if ($fields[0] == $v['id']) {
            $str .= "<option selected=''";
        } else {
            $str .= "<option";
        }
        $str .= " value='{$v['id']}'>" . str_pad($v['id'], 10, '.', STR_PAD_RIGHT) . $v['title'] . '</option>';
    }
    $str .= '</select>';
    return $str;
}

/**
 * 数字价格 转 中文
 * @param $p
 * @return string
 */
function price2cn($p) {
    if ($p >= 10000) {
        return $p / 10000 . '万';
    } else {
        return $p;
    }
}

/**
 * 课程时间 转 中文状态
 * @param $a
 * @return string
 */
function classtime2cnstatus($a) {
    if ($a < time()) {
        return '预约中';
    } else {
        return '报名结束';
    }
}

function sex2cn($a) {
    switch ($a) {
        case 1:
            return '男';
            break;
        case 2:
            return '女';
            break;
        default:
            return '保密';
    }
}

function json_exit($data, $code = 100, $desc = null) {
    header('content-type: text/javascript;');
    echo json_encode(['status' => (int)$code, 'data' => $data, 'desc' => $desc], JSON_UNESCAPED_UNICODE);
    exit();
}

function get_ref($path = APP_PATH) {
    if (isset($_SERVER['HTTP_REFERER'])) {
        $ref = $_SERVER['HTTP_REFERER'];

        if (preg_match("/a=doclass/", $ref)) {
            return APP_PATH;
        }

        return 'javascript:history.go(-1);';
    } else {
        return $path;
    }
}

function my_explode($a, $b, $c = null) {
    $r = explode($a, $b);
    if ($r[0] == '') {
        return [];
    }

    return $r;
}

/**
 * 星星 => html
 * @param $a
 * @return string
 */
function star2html($a) {
    if (is_array($a)) {
        $star = 0;
        foreach ($a as $r) {
            $star += $r['star'];
        }
        $a = round($star / count($a));
    }

    $str = '';

    $a = min($a, 5);
    $a = max($a, 1);

    for ($i = 0; $i < $a; $i++) {
        $str .= '<i class="on"></i>';
    }

    for ($i = $a; $i < 5; $i++) {
        $str .= '<i></i>';
    }

    return $str;
}